package edu.tjut.tjutcommunity.entity;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;

@NoArgsConstructor
@Data
public class Message<T> implements Serializable {
    private Meta meta;
    private T data;

    private Message(Meta meta,T data){
        this.meta=meta;
        this.data = data;
    }
    //成功的响应
    public static Message ok(Object result) {
        return new Message(new Meta(HttpCodeConstant.SUCCESS), result);
    }

    /**
     * 构造空信息的
     * @param meta
     * @return
     */
    public static Message EMPTY(Meta meta) {
        return new Message(meta, null);
    }
    public static Message ok() {
        return EMPTY(new Meta(HttpCodeConstant.NO_CONTENT));
    }
    //结果为空的响应
    public static Message empty() {
        return EMPTY(new Meta(HttpCodeConstant.NO_CONTENT));
    }

    //token过期
    public static Message tokenHasExpired() {
        return EMPTY(new Meta(HttpCodeConstant.TOKEN_EXPIRES));
    }

    //token非法
    public static Message tokenInvalid() {
        return EMPTY(new Meta(HttpCodeConstant.UNAUTHORIZED));
    }


    //无参的失败响应
    public static Message failed() {
        return EMPTY(new Meta(HttpCodeConstant.METHOD_NOT_ALLOWED));
    }

    //师生服务的cookie过期了，需要重新登录
    public static Message cookieHasExpired() {
        return EMPTY(new Meta(HttpCodeConstant.COLLEGE_TOKEN_EXPIRES));
    }

    //有参的失败响应
    public static Message failed(String msg) {
        return new Message(new Meta(HttpCodeConstant.FAILED), Collections.singletonMap("msg", msg));
    }

    //未登录的
    public static Message Unauthorized() {
        return EMPTY(new Meta(HttpCodeConstant.UNAUTHORIZED));
    }

    //被禁止的，权限不足
    public static Message InsufficientPermissions() {
        return EMPTY(new Meta(HttpCodeConstant.UNAUTHORIZED));
    }
    public static Message HasStar() {
        return EMPTY(new Meta(HttpCodeConstant.HAS_STAR));
    }
    public static Message NoStar() {
        return EMPTY(new Meta(HttpCodeConstant.NO_STAR));
    }

    public int getCode() {
        return meta.getStatus();
    }



    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class Meta {
        Integer status;
        String description;
        public Meta(HttpCodeConstant httpCodeConstant){
            this.status=httpCodeConstant.getValue();
            this.description=httpCodeConstant.getFiled();

        }
    }
}
